﻿
#region Usings

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;
using System.Xml;

#endregion

namespace FasterWPF.Generators
{
    public static class Generators
    {
        public static T CloneElement<T>(T source)
        {
            string sourceString = XamlWriter.Save(source);
            StringReader stringReader = new StringReader(sourceString);
            XmlReader xmlReader = XmlTextReader.Create(stringReader, new XmlReaderSettings());
            return (T)XamlReader.Load(xmlReader);
        }

        public delegate bool Condition(string Arg);

        //1. Adding Composites

        #region Parent Grid

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent Grid. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startRow"></param>
        /// <param name="startCol"></param>
        /// <param name="endRow"></param>
        /// <param name="endCol"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(Grid chain, int childRow, int childColumn, int startRow, int startCol, int endRow, int endCol, T prototype, Action<T> constructorAction)
               where T: FrameworkElement, new()
        {
            int NumColumns = chain.ColumnDefinitions.Count;
            int NumRows = chain.RowDefinitions.Count;
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(NumRows, (row =>
            {
                IterHelpers.ApplyLoop(NumColumns, (col =>
                {
                    if (startRow <= row && endRow >= row && startCol <= col && endCol >= col)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(sp);

                        string current = Guid.NewGuid().ToString();
                        keys.Add(current);

                        chain.BeginComposite<Grid>(current).AddExisting<T, Grid>(childRow, childColumn, sp).EndComposite<Grid, GridArgs>(new GridArgs(row, col));
                    }
                }));
            }));

            return keys;
        }
        
        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent Grid. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allows passing in custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startRow"></param>
        /// <param name="startCol"></param>
        /// <param name="endRow"></param>
        /// <param name="endCol"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(Grid chain, int childRow, int childColumn, int startRow, int startCol, int endRow, int endCol, T prototype, Action<T> constructorAction, GridSettings compositeGridSettings)
               where T: FrameworkElement, new()
        {
            int NumColumns = chain.ColumnDefinitions.Count;
            int NumRows = chain.RowDefinitions.Count;
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(NumRows, (row =>
            {
                IterHelpers.ApplyLoop(NumColumns, (col =>
                {
                    if (startRow <= row && endRow >= row && startCol <= col && endCol >= col)
                    {                        
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(sp);

                        string current = Guid.NewGuid().ToString();
                        keys.Add(current);

                        chain.BeginComposite<Grid>(current).AddExisting<T, Grid>(childRow, childColumn, sp).EndComposite<Grid, GridArgs>(new GridArgs(row, col), compositeGridSettings);
                    }
                }));
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent Grid. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startRow"></param>
        /// <param name="startCol"></param>
        /// <param name="endRow"></param>
        /// <param name="endCol"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(Grid chain, int childRow, int childColumn, int startRow, int startCol, int endRow, int endCol, T prototype, Action<int, int, T> constructorAction)
               where T : FrameworkElement, new()
        {
            int NumColumns = chain.ColumnDefinitions.Count;
            int NumRows = chain.RowDefinitions.Count;
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(NumRows, (row =>
            {
                IterHelpers.ApplyLoop(NumColumns, (col =>
                {
                    if (startRow <= row && endRow >= row && startCol <= col && endCol >= col)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(row, col, sp);

                        string current = Guid.NewGuid().ToString();
                        keys.Add(current);

                        chain.BeginComposite<Grid>(current).AddExisting<T, Grid>(childRow, childColumn, sp).EndComposite<Grid, GridArgs>(new GridArgs(row, col));
                    }
                }));
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent Grid. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allows passing in custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startRow"></param>
        /// <param name="startCol"></param>
        /// <param name="endRow"></param>
        /// <param name="endCol"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(Grid chain, int childRow, int childColumn, int startRow, int startCol, int endRow, int endCol, T prototype, Action<int, int, T> constructorAction, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            int NumColumns = chain.ColumnDefinitions.Count;
            int NumRows = chain.RowDefinitions.Count;
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(NumRows, (row =>
            {
                IterHelpers.ApplyLoop(NumColumns, (col =>
                {
                    if (startRow <= row && endRow >= row && startCol <= col && endCol >= col)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(row, col, sp);

                        string current = Guid.NewGuid().ToString();
                        keys.Add(current);

                        chain.BeginComposite<Grid>(current).AddExisting<T, Grid>(childRow, childColumn, sp).EndComposite<Grid, GridArgs>(new GridArgs(row, col), compositeGridSettings);
                    }
                }));
            }));

            return keys;
        }

        #endregion

        #region Parent StackPanel

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent StackPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(StackPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                   if (startIndex <= i && endIndex >= i)
                   {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        string current = Guid.NewGuid().ToString();
                        keys.Add(current);

                        chain.BeginComposite<StackPanel>(current).AddExisting<T, StackPanel>(childRow, childColumn, sp).EndComposite<StackPanel, StackPanelArgs>(null);
                   }
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent StackPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing in custom Orientation.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="orient"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(StackPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, Orientation orient)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex+1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<StackPanel>(current).AddExisting<T, StackPanel>(childRow, childColumn, sp).EndComposite<StackPanel, StackPanelArgs>(new StackPanelArgs(orient));
                }
            }));

            return keys;
        }


        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent StackPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing in custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(StackPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<StackPanel>(current).AddExisting<T, StackPanel>(childRow, childColumn, sp).EndComposite<StackPanel, StackPanelArgs>(null, compositeGridSettings);
                }
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent StackPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing in custom Orientation and GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="orient"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(StackPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, Orientation orient, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<StackPanel>(current).AddExisting<T, StackPanel>(childRow, childColumn, sp).EndComposite<StackPanel, StackPanelArgs>(new StackPanelArgs(orient), compositeGridSettings);
                }
            }));

            return keys;
        }
        #endregion

        #region Parent WrapPanel

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent WrapPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(WrapPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<WrapPanel>(current).AddExisting<T, WrapPanel>(childRow, childColumn, sp).EndComposite<WrapPanel, WrapPanelArgs>(null);
                }
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent WrapPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing in custom Orientation.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="orient"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(WrapPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, Orientation orient)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<WrapPanel>(current).AddExisting<T, WrapPanel>(childRow, childColumn, sp).EndComposite<WrapPanel, WrapPanelArgs>(new WrapPanelArgs(orient));
                }
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent WrapPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing in custom GridSettings
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(WrapPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<WrapPanel>(current).AddExisting<T, WrapPanel>(childRow, childColumn, sp).EndComposite<WrapPanel, WrapPanelArgs>(null, compositeGridSettings);
                }
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent WrapPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing in custom Orientation and GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="orient"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(WrapPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, Orientation orient, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<WrapPanel>(current).AddExisting<T, WrapPanel>(childRow, childColumn, sp).EndComposite<WrapPanel, WrapPanelArgs>(new WrapPanelArgs(orient), compositeGridSettings);
                }
            }));

            return keys;
        }
        #endregion

        #region Parent DockPanel

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(DockPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<DockPanel>(current).AddExisting<T, DockPanel>(childRow, childColumn, sp).EndComposite<DockPanel, DockArgs>(null);
                }
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(DockPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Func<int, T, Dock> constructorAction)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    Dock dock = constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<DockPanel>(current).AddExisting<T, DockPanel>(childRow, childColumn, sp).EndComposite<DockPanel, DockArgs>(new DockArgs(dock));
                }
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing in custom Dock position.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="dock"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(DockPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, System.Windows.Controls.Dock dock)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<DockPanel>(current).AddExisting<T, DockPanel>(childRow, childColumn, sp).EndComposite<DockPanel, DockArgs>(new DockArgs(dock));
                }
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(DockPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<DockPanel>(current).AddExisting<T, DockPanel>(childRow, childColumn, sp).EndComposite<DockPanel, DockArgs>(null, compositeGridSettings);
                }
            }));

            return keys;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Composites in a Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(DockPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Func<int, T, Dock> constructorAction, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    Dock dock = constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<DockPanel>(current).AddExisting<T, DockPanel>(childRow, childColumn, sp).EndComposite<DockPanel, DockArgs>(new DockArgs(dock), compositeGridSettings);
                }
            }));

            return keys;
        }

        /// <summary>
        ///  Add Elements Cloned From a Prototype to Composites in a Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow passing in custom Dock position and GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="childColumn"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="dock"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static List<string> AddComposites<T>(DockPanel chain, int childRow, int childColumn, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, System.Windows.Controls.Dock dock, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            List<string> keys = new List<string>();

            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                    T sp = new T();
                    sp = CloneElement<T>(prototype);

                    constructorAction(i, sp);

                    string current = Guid.NewGuid().ToString();
                    keys.Add(current);

                    chain.BeginComposite<DockPanel>(current).AddExisting<T, DockPanel>(childRow, childColumn, sp).EndComposite<DockPanel, DockArgs>(new DockArgs(dock), compositeGridSettings);
                }
            }));

            return keys;
        }

        #endregion

        #region Parent GroupBox

        public static List<string> AddLabelsAndFields(GroupBox chain, List<string> labels, int childRow, int startIndex, int endIndex, double rowHeightInPixels, Label prototype, Action<int, Label> constructorAction, TextBox prototype2, Action<int, TextBox> constructorAction2)
        {
            int counter = 0;
            List<string> keys = new List<string>();

            StackPanel inside = new StackPanel();
            inside.Initialize(double.NaN, double.NaN, ContainerType.Grid, Orientation.Vertical);
            inside.SetCompositeGridDimensions(1, 2, 0.50, GridUnitType.Star, rowHeightInPixels, GridUnitType.Pixel);

            chain.BeginComposite<GroupBox>().AddExisting<StackPanel, GroupBox>(0, 0, inside).EndComposite<GroupBox, GroupBoxArgs>(null);
            
            IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
            {
                if (startIndex <= i && endIndex >= i)
                {
                        Label sp = new Label();
                        sp = CloneElement<Label>(prototype);
                        sp.Content = labels[counter];

                        constructorAction(i, sp);

                        TextBox tx2 = new TextBox();
                        tx2 = CloneElement<TextBox>(prototype2);

                        constructorAction2(i, tx2);

                        string current = Guid.NewGuid().ToString();
                        keys.Add(current);

                        inside.BeginComposite<StackPanel>(current).AddExisting<Label, StackPanel>(0, 0, sp).AddExisting<TextBox, StackPanel>(0, 1, tx2).EndComposite<StackPanel, StackPanelArgs>(null);

                        counter++;
                }
            }));

           // inside.SortByText<StackPanel>(0, 1, CommonExt.SortOrder.Desc, CommonExt.SortControlType.TextBox);
          // inside.SortByText1AndThenBy2<StackPanel>(0, 1, 0, 1, CommonExt.SortOrder.Asc, CommonExt.SortOrder.Asc, CommonExt.SortControlType.TextBox);
          
            return keys;
        }
               
        #endregion

        //ListBox???

        //Canvas???


        //2. Adding Children

        #region Parent Grid

        /// <summary>
        /// Add Elements Cloned From a Prototype to Children in a Range on a Parent Composite. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="startRow"></param>
        /// <param name="startCol"></param>
        /// <param name="endRow"></param>
        /// <param name="endCol"></param>
        /// <param name="parentRow"></param>
        /// <param name="parentColumn"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInRange<T>(Grid chain, int startRow, int startCol, int endRow, int endCol, int parentRow, int parentColumn, T prototype, Action<T> constructorAction)
               where T : FrameworkElement, new()
        {
            int NumColumns = chain.ColumnDefinitions.Count;
            int NumRows = chain.RowDefinitions.Count;
       
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<Grid, GridArgs>(current, new GridArgs(parentRow, parentColumn), cc => {

            IterHelpers.ApplyLoop(NumRows, (row =>
            {
                IterHelpers.ApplyLoop(NumColumns, (col =>
                {
                    if (startRow <= row && endRow >= row && startCol <= col && endCol >= col)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(sp);

                        cc.AddExisting<T, Grid>(current, row, col, sp);
                    }
                }));
            }));
            });

            return current;
        }
        
        /// <summary>
        /// Add Elements Cloned From a Prototype to Children in a Range on a Parent Composite. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allows passing in custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="startRow"></param>
        /// <param name="startCol"></param>
        /// <param name="endRow"></param>
        /// <param name="endCol"></param>
        /// <param name="parentRow"></param>
        /// <param name="parentColumn"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static string AddChildrenInRange<T>(Grid chain, int startRow, int startCol, int endRow, int endCol, int parentRow, int parentColumn, T prototype, Action<T> constructorAction, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            int NumColumns = chain.ColumnDefinitions.Count;
            int NumRows = chain.RowDefinitions.Count;

            string current = Guid.NewGuid().ToString();

            chain.AddComposite<Grid, GridArgs>(current, new GridArgs(parentRow, parentColumn), compositeGridSettings, cc =>
            {
                IterHelpers.ApplyLoop(NumRows, (row =>
                {
                    IterHelpers.ApplyLoop(NumColumns, (col =>
                    {
                        if (startRow <= row && endRow >= row && startCol <= col && endCol >= col)
                        {
                            T sp = new T();
                            sp = CloneElement<T>(prototype);

                            constructorAction(sp);

                            cc.AddExisting<T, Grid>(current, row, col, sp);
                        }
                    }));
                }));
            });

            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Children in a Range on a Parent Composite. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="startRow"></param>
        /// <param name="startCol"></param>
        /// <param name="endRow"></param>
        /// <param name="endCol"></param>
        /// <param name="parentRow"></param>
        /// <param name="parentColumn"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInRange<T>(Grid chain, int startRow, int startCol, int endRow, int endCol, int parentRow, int parentColumn, T prototype, Action<int, int, T> constructorAction)
               where T : FrameworkElement, new()
        {
            int NumColumns = chain.ColumnDefinitions.Count;
            int NumRows = chain.RowDefinitions.Count;

            string current = Guid.NewGuid().ToString();

            chain.AddComposite<Grid, GridArgs>(current, new GridArgs(parentRow, parentColumn), cc =>
            {
                IterHelpers.ApplyLoop(NumRows, (row =>
                {
                    IterHelpers.ApplyLoop(NumColumns, (col =>
                    {
                        if (startRow <= row && endRow >= row && startCol <= col && endCol >= col)
                        {
                            T sp = new T();
                            sp = CloneElement<T>(prototype);

                            constructorAction(row, col, sp);

                            cc.AddExisting<T, Grid>(current, row, col, sp);
                        }
                    }));
                }));
            });

            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to Children in a Range on a Parent Composite. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allows passing in custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="startRow"></param>
        /// <param name="startCol"></param>
        /// <param name="endRow"></param>
        /// <param name="endCol"></param>
        /// <param name="parentRow"></param>
        /// <param name="parentColumn"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="compositeGridSettings"></param>
        /// <returns></returns>
        public static string AddChildrenInRange<T>(Grid chain, int startRow, int startCol, int endRow, int endCol, int parentRow, int parentColumn, T prototype, Action<int, int, T> constructorAction, GridSettings compositeGridSettings)
               where T : FrameworkElement, new()
        {
            int NumColumns = chain.ColumnDefinitions.Count;
            int NumRows = chain.RowDefinitions.Count;

            string current = Guid.NewGuid().ToString();

            chain.AddComposite<Grid, GridArgs>(current, new GridArgs(parentRow, parentColumn), compositeGridSettings, cc =>
            {
                IterHelpers.ApplyLoop(NumRows, (row =>
                {
                    IterHelpers.ApplyLoop(NumColumns, (col =>
                    {
                        if (startRow <= row && endRow >= row && startCol <= col && endCol >= col)
                        {
                            T sp = new T();
                            sp = CloneElement<T>(prototype);

                            constructorAction(row, col, sp);

                            cc.AddExisting<T, Grid>(current, row, col, sp);
                        }
                    }));
                }));
            });

            return current;
        }
        
        #endregion
        
        #region Parent StackPanel

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Column Range on a Parent StackPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInColumnRange<T>(StackPanel chain, int childRow, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<StackPanel, StackPanelArgs>(current, null, cc => {

                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);
                        
                        cc.AddExisting<T, StackPanel>(current, childRow, i, sp);
                    }
                }));
            } );
            
            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Column Range on a Parent StackPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="gridCompositeSettings"></param>
        /// <returns></returns>
        public static string AddChildrenInColumnRange<T>(StackPanel chain, int childRow, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, GridSettings gridCompositeSettings)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<StackPanel, StackPanelArgs>(current, null, gridCompositeSettings, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, StackPanel>(current, childRow, i, sp);
                    }
                }));
            });

            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Row Range on a Parent StackPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInRowRange<T>(StackPanel chain, int startIndex, int endIndex, int childColumn, T prototype, Action<int, T> constructorAction)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<StackPanel, StackPanelArgs>(current, null, cc =>
            {

                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, StackPanel>(current, i, childColumn, sp);
                    }
                }));
            });

            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Row Range on a Parent StackPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="gridCompositeSettings"></param>
        /// <returns></returns>
        public static string AddChildrenInRowRange<T>(StackPanel chain, int startIndex, int endIndex, int childColumn, T prototype, Action<int, T> constructorAction, GridSettings gridCompositeSettings)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<StackPanel, StackPanelArgs>(current, null, gridCompositeSettings, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, StackPanel>(current, i, childColumn, sp);
                    }
                }));
            });

            return current;
        }
        #endregion

        #region Parent WrapPanel

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Column Range on a Parent WrapPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInColumnRange<T>(WrapPanel chain, int childRow, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<WrapPanel, WrapPanelArgs>(current, null, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, WrapPanel>(current, childRow, i, sp);
                    }
                }));
            });

            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Column Range on a Parent WrapPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="gridCompositeSettings"></param>
        /// <returns></returns>
        public static string AddChildrenInColumnRange<T>(WrapPanel chain, int childRow, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, GridSettings gridCompositeSettings)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<WrapPanel, WrapPanelArgs>(current, null, gridCompositeSettings, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, WrapPanel>(current, childRow, i, sp);
                    }
                }));
            });

            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Row Range on a Parent WrapPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInRowRange<T>(WrapPanel chain, int startIndex, int endIndex, int childColumn, T prototype, Action<int, T> constructorAction)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<WrapPanel, WrapPanelArgs>(current, null, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, WrapPanel>(current, i, childColumn, sp);
                    }
                }));
            });

            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Row Range on a Parent WrapPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <param name="gridCompositeSettings"></param>
        /// <returns></returns>
        public static string AddChildrenInRowRange<T>(WrapPanel chain, int startIndex, int endIndex, int childColumn, T prototype, Action<int, T> constructorAction, GridSettings gridCompositeSettings)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<WrapPanel, WrapPanelArgs>(current, null, gridCompositeSettings, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, WrapPanel>(current, i, childColumn, sp);
                    }
                }));
            });

            return current;
        }
        #endregion

        #region Parent DockPanel

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Column Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInColumnRange<T>(DockPanel chain, int childRow, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<DockPanel, DockArgs>(current, null, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, DockPanel>(current, childRow, i, sp);
                    }
                }));
            });

            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Row Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="childColumn"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInRowRange<T>(DockPanel chain, int startIndex, int endIndex, int childColumn, T prototype, Action<int, T> constructorAction)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<DockPanel, DockArgs>(current, null, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, DockPanel>(current, i, childColumn, sp);
                    }
                }));
            });

            return current;
        }
        
        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Column Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="childRow"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInColumnRange<T>(DockPanel chain, int childRow, int startIndex, int endIndex, T prototype, Action<int, T> constructorAction, GridSettings gridCompositeSettings)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<DockPanel, DockArgs>(current, null, gridCompositeSettings, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, DockPanel>(current, childRow, i, sp);
                    }
                }));
            });

            return current;
        }

        /// <summary>
        /// Add Elements Cloned From a Prototype to a Single Composite as Children in a Row Range on a Parent DockPanel. Constructor action is provided to allow Initialization, BeginSettings...EndSettings, etc. Allow custom GridSettings.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="childColumn"></param>
        /// <param name="prototype"></param>
        /// <param name="constructorAction"></param>
        /// <returns></returns>
        public static string AddChildrenInRowRange<T>(DockPanel chain, int startIndex, int endIndex, int childColumn, T prototype, Action<int, T> constructorAction, GridSettings gridCompositeSettings)
       where T : FrameworkElement, new()
        {
            string current = Guid.NewGuid().ToString();

            chain.AddComposite<DockPanel, DockArgs>(current, null, gridCompositeSettings, cc =>
            {
                IterHelpers.ApplyLoop(startIndex, (endIndex + 1), (i =>
                {
                    if (startIndex <= i && endIndex >= i)
                    {
                        T sp = new T();
                        sp = CloneElement<T>(prototype);

                        constructorAction(i, sp);

                        cc.AddExisting<T, DockPanel>(current, i, childColumn, sp);
                    }
                }));
            });

            return current;
        }
                
        #endregion

    }//end of class
}//end of namespace
